package be.grubb.larp;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import be.grubb.larp.entities.User;

public class App {
	private Log log = LogFactory.getLog(App.class);
	private EntityManagerFactory entityManagerFactory;
	private EntityManager entityManager;

	public static void main(String[] args) {
		new App().run();
	}

	private void run() {
		log.info("start");

		entityManagerFactory = Persistence.createEntityManagerFactory("be.grubb.larp.jpa");

		entityManager = entityManagerFactory.createEntityManager();
		entityManager.getTransaction().begin();
		entityManager.persist(new User("sa", "sa"));
		entityManager.persist(new User("admin", "admin"));
		entityManager.getTransaction().commit();
		entityManager.close();

		entityManager = entityManagerFactory.createEntityManager();

		entityManager = entityManagerFactory.createEntityManager();
		entityManager.getTransaction().begin();
		List<User> result = entityManager.createQuery("from User", User.class).getResultList();
		for (User user : result) {
			System.out.println("User (" + user.getName() + ", " + user.getPassword() + ", " + user.getDate() + ")");
		}
		entityManager.getTransaction().commit();
		entityManager.close();

		entityManagerFactory.close();

		log.info("stop");
	}
}
